package com.woniuxy.service.impl;

import com.woniuxy.dao.RbacManagerDao;
import com.woniuxy.entity.RbacManager;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.RbacManagerService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 马宇航
 * @Do: TODO
 * @DateTime: 22/05/23/0023 下午 2:36
 * @Component: 成都蜗牛学苑
 **/
@Service
public class RbacManagerServiceImpl implements RbacManagerService {
    @Autowired(required = false)
    RbacManagerDao rbacManagerDao;
    @Override
    public List<RbacManager> findAll() {
       return rbacManagerDao.findAll();
    }

    @Override
    public int deleteOne(int id) {
        return rbacManagerDao.deleteOne(id);
    }

    @Override
    public int saveOne(RbacManager rbacManager) {
        //判断 当前的账号数据库不存在
        int i = rbacManagerDao.determineIfTheAccountIsDuplicated(rbacManager.getAccount());
        if (i>=1){
            return 0;
        }
        //密码加密
        String password = rbacManager.getPassword();
        SimpleHash simpleHash = new SimpleHash("MD5",password,rbacManager.getAccount());
        String newPassword = simpleHash.toHex();
        rbacManager.setPassword(newPassword);
        return rbacManagerDao.saveOne(rbacManager);
    }

    @Override
    public int updateOne(RbacManager rbacManager) {
        //判断 当前的账号数据库不存在
        int i = rbacManagerDao.determineIfTheAccountIsDuplicated(rbacManager.getAccount());
        if (i>=1){
            return 0;
        }
        String password = rbacManager.getPassword();
        SimpleHash simpleHash = new SimpleHash("MD5",password,rbacManager.getAccount());
        String newPassword = simpleHash.toHex();
        rbacManager.setPassword(newPassword);
        return rbacManagerDao.updateOne(rbacManager);
    }

    @Override
    public int resetPassword(String account) {
        String password = "123456";
        SimpleHash simpleHash = new SimpleHash("MD5",password,account);
        String newPassword = simpleHash.toHex();
        return rbacManagerDao.resetPassword(account,newPassword);
    }
}
